package com.kc.sjwh.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.ZhzxpzxxDao;
import com.kc.sjwh.dao.CzllzDao;
import com.kc.sjwh.dao.CzllzcyDao;
import com.kc.sjwh.dao.ZhzxpzgxhzbDao;
import com.kc.sjwh.dao.ZhzxpzgxhzbsjDao;
import com.kc.sjwh.entity.po.Zhzxpzxx;
import com.kc.sjwh.entity.po.Zhzxpzgxhzbsj;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;

/**
 * 处置力量页 service
 * @author xianding
 * @date 2017年11月28日
 */
@Service
public class ZhzxpzxxService {
	static Logger logger = Logger.getLogger(ZhzxpzxxService.class);
	@Autowired
	ZhzxpzxxDao zhzxpzxxDao;
	@Autowired
	ZhzxpzgxhzbDao zhzxpzgxhzbDao;
	@Autowired
	ZhzxpzgxhzbsjDao zhzxpzgxhzbsjDao;
	@Autowired
	CzllzDao czllzDao;
	@Autowired
	CzllzcyDao czllzcyDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Zhzxpzxx> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("t.gxsj desc");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("startTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj >= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("endTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj <= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t.zhzxbh like ? or t.zhzxmc like ? or t.zhzxqmc like ? ".replace("?", "'%"+v+"%'"));
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.put("t.ssdwdm", v);
		}
		v = param.get("sx");
		if(StringUtils.isNotEmpty(v)) {
			q.put("t.zhzxsxdm", v);
		}
		
		return zhzxpzxxDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Zhzxpzxx one) {
		one.setCjsj(new Date());	
		zhzxpzxxDao.add(one);

	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Zhzxpzxx one) {
		one.setGxsj(new Date());	
		zhzxpzxxDao.update(one, "zhzxbh");

	}

	/**
	 * 删除
	 * @param id
	 */
	@Transactional
	public void delete(String id) {
		//级联删除 none
		Zhzxpzxx one =new Zhzxpzxx();
		one.setZhzxbh(id);
		one.setSybz("N");
		one.setGxsj(new Date());
		zhzxpzxxDao.update(one, "zhzxbh");
//		//删除
//		int n = zhzxpzxxDao.delete(id);
//		if(n == 0) {
//			throw new IllegalArgumentException("要删除的记录不存在");
//		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Zhzxpzxx form(String id) {
		return id==null ? null : zhzxpzxxDao.getOne(new QueryParam().put("zhzxbh", id));
	}


	/**
	 * 新增
	 * @param one
	 */
	public void addCfg(Zhzxpzgxhzbsj one) {
		 zhzxpzgxhzbsjDao.add(one);
	}
	
	/**
	 * 修改
	 * @param one
	 */
	public void updateCfg(Zhzxpzgxhzbsj one,String bh) {
		one.setBh(bh);
		zhzxpzgxhzbsjDao.update(one,"bh");
	}

	public String getModelByIp(String zbxbh, String zhzxbh) {
		List<Zhzxpzgxhzbsj> list=zhzxpzgxhzbsjDao.getList(new QueryParam()
				.put("zhzxbh", zhzxbh)
				.put("zbxbh", zbxbh));
		if (list.size()==0) {
			return "";
		}
		return list.get(0).getZbxz();
	}

	public Zhzxpzgxhzbsj getCfg(Zhzxpzgxhzbsj one) {
		return zhzxpzgxhzbsjDao.getOne(new QueryParam()
				.put("zhzxbh",one.getZhzxbh())
				.put("zbxbh", one.getZbxbh()));
		
	}

}
